package com.umeng.comm.ui.activities;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.qianxx.draw.R;
import com.qianxx.base.BaseAty;
import com.qianxx.base.utils.ToastUtil;
import com.umeng.fb.FeedbackAgent;
import com.umeng.fb.SyncListener;
import com.umeng.fb.model.Conversation;
import com.umeng.fb.model.Reply;

import java.util.List;

/**
 * Created by summersoft on 2016/5/12.
 */
public class OpinionActivity extends BaseAty{
    private Conversation comversation;
    // 反馈内容
    private EditText mEtidea;

    // 反馈成功
    private TextView suc;

    // 说明1
    private TextView shou1;

    // 说明2
    private TextView shou2;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.opinion);
        initUI();
    }

    @Override
    public void topRightClick(View v) {
        super.topRightClick(v);
        if (ifPressed()) {
            return;
        }
        if (checkInput()) {
            loadData();
        }
    }

    private void initUI() {

        showTitle("意见反馈");
        showTopRight("确定");
        tvTopRight.setVisibility(View.VISIBLE);
        comversation = new FeedbackAgent(this).getDefaultConversation();
        suc = (TextView) findViewById(R.id.opinion_success);
        shou1 = (TextView) findViewById(R.id.opinion_shouming1);
        shou2 = (TextView) findViewById(R.id.opinion_shouming2);
        suc.setVisibility(View.GONE);
        shou1.setVisibility(View.GONE);
        shou2.setVisibility(View.GONE);
        mEtidea = (EditText) findViewById(R.id.opinion_et);
        mEtidea.setVisibility(View.VISIBLE);
    }

    private void loadData() {
        comversation.addUserReply(mEtidea.getText().toString());
        comversation.sync(new SyncListener() {

            @Override
            public void onSendUserReply(List<Reply> replyList) {
                mEtidea.getEditableText().clear();
                mEtidea.getEditableText().clear();
                mEtidea.setVisibility(View.GONE);
                tvTopRight.setVisibility(View.GONE);
                suc.setVisibility(View.VISIBLE);
                shou1.setVisibility(View.VISIBLE);
                shou2.setVisibility(View.VISIBLE);

            }

            @Override
            public void onReceiveDevReply(List<Reply> replyList) {
            }
        });
    }

    private boolean checkInput() {

        if (TextUtils.isEmpty(mEtidea.getText().toString())) {
            ToastUtil.getInstance().toast("请输入反馈内容");
            return false;
        }
        if (mEtidea.getText().toString().length() > 400) {
            ToastUtil.getInstance().toast("超过字数，请重新编辑");
            return false;
        }

        return true;
    }

}
